//
//  ViewController.m
//  rili
//
//  Created by 张超 on 2017/9/20.
//  Copyright © 2017年 张超. All rights reserved.
//

#import "ViewController.h"
#import "riliView.h"
#import "PickerChoiceView.h"
#import "ZLRiliModel.h"
#import "rilisModel.h"
#import "ZLRiliCell.h"

@interface ViewController ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) NSMutableArray *array;



@property (nonatomic, weak) IBOutlet UIButton *cellTitle;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"工作日历";
    
    self.tableView.delegate = self;
    
    self.tableView.dataSource = self;
    
    self.tableView.estimatedSectionHeaderHeight =  60;
    self.tableView.sectionHeaderHeight = 60;
    
    
    self.tableView.backgroundColor = [UIColor whiteColor];
    
    self.array = [NSMutableArray array];
    
    ZLRiliModel *model = [[ZLRiliModel alloc] init];
    
    model.title = @"请选择";
    
    model.index = 0;
    
    model.rilis = [NSMutableArray array];
    
    rilisModel *rilis = [[rilisModel alloc] init];
    
    rilis.title = @"请选择";
    
    [model.rilis addObject:rilis];
    
    [self.array addObject:model];
//    [self.tableView registerNib:[UINib nibWithNibName:@"riliView" bundle:nil] forHeaderFooterViewReuseIdentifier:riliViewHeader];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"添加分组" style:UIBarButtonItemStylePlain target:self action:@selector(addClick)];
    
    
    UITapGestureRecognizer* singleRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(endClick)];
    
    singleRecognizer.numberOfTapsRequired = 1;
    
    [self.tableView addGestureRecognizer:singleRecognizer];
}
- (void)endClick {
    [self.tableView endEditing: YES];
}
- (void)addClick {
    
    ZLRiliModel *model = [[ZLRiliModel alloc] init];
    
    model.title = @"请选择";
    
    model.index = 0;
    
    model.rilis = [NSMutableArray array];
    
    [self.array addObject:model];

    
    [self.tableView reloadData];
    
    //一个section刷新
//    
//    NSIndexSet *indexSet=[[NSIndexSet alloc]initWithIndex:self.array.count]; //你需要更新的组数
//    
//    [self.tableView reloadSections:indexSet withRowAnimation:UITableViewRowAnimationAutomatic];  //collection
    
}
- (IBAction)btnClick:(id)sender {
    
  
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
//- (nullable UITableViewHeaderFooterView *)headerViewForSection:(NSInteger)section NS_AVAILABLE_IOS(6_0);
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    
    ZLRiliModel *model = self.array[section];
    
    riliView *rili = [riliView riliview:tableView];
                      
    
    rili.riliModel = model;
    
    rili.btnBlock = ^(UIButton *sender) {
        
        PickerChoiceView *picker = [PickerChoiceView pickerChoiceViewFrame:self.view.bounds andArray:@[@"现场检验",@"填写报告",@"开会",@"在途",@"其他"] andTitle:@"请选择" andRemoveBlock:^(NSString *str) {
            
            model.title = str;
            
            [sender setTitle:str forState:UIControlStateNormal];
            
            [sender setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
            
        }];
        
        [self.view addSubview:picker];
    } ;
    
    rili.addBlock = ^(UIButton *sender) {
        
        model.index += 1;
        
        rilisModel *rilis = [[rilisModel alloc] init];
        
        rilis.title = @"请选择";
        
        [model.rilis addObject:rilis];
        
        //一个section刷新
        
        NSIndexSet *indexSet=[[NSIndexSet alloc]initWithIndex:section]; //你需要更新的组数
        
        [self.tableView reloadSections:indexSet withRowAnimation:UITableViewRowAnimationAutomatic];  //collection
    };
    
    rili.endBlock = ^(UITextField *sender) {
        
        model.timer = sender.text;
    };
    
    return rili;
    
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.array.count;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    ZLRiliModel *model = self.array[section];

    return model.index;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    ZLRiliCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cells"];
    
    ZLRiliModel *model = self.array[indexPath.section];
    
    rilisModel *rilis = model.rilis[indexPath.row];
    
    cell.rilis = rilis;
   
    cell.cellBlock = ^(UIButton *sender){
      
        PickerChoiceView *picker = [PickerChoiceView pickerChoiceViewFrame:self.view.bounds andArray:@[@"现场检验",@"填写报告",@"开会",@"在途",@"其他"] andTitle:@"请选择" andRemoveBlock:^(NSString *str) {
            
            [sender setTitle:str forState:UIControlStateNormal];
            
            [sender setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
            
            rilis.title = str;
            
        }];
        
        [self.view addSubview:picker];

    };
    cell.cellEndBlock = ^(UITextField *sender) {
        
        rilis.timer = sender.text;
    };
    return cell;
}

@end
